const Error = require('./error')
const storage = require('./storage.js')
const request = require('./request')
const {
    isObject,
    isString,
    isNumber,
    isUndefined,
    isBoolean,
    isArray
} = require('./dataType.js')

const entryAndExit = class entryAndExit {
    constructor() {}

    get(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EAE.GET.url,
                MS._config.parameters.EAE.GET.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    query(options) {
        let params = {
            pageSize: 1,
            pageNum: 10
        }
        if(!isUndefined(options)) {
            if(!isUndefined(options.applicantId) && isString(options.applicantId)) {
                params.applicantId = options.applicantId;
            }
            if(!isUndefined(options.type) && isNumber(options.type)) {
                params.type = options.type;
            }
            if(!isUndefined(options.status) && isNumber(options.status)) {
                params.status = options.status;
            }
            if(!isUndefined(options.statusArray) && isArray(options.statusArray)) {
                params.statusArray = JSON.stringify(options.statusArray);
            }
            if(!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
                params.pageNum = options.pageNum;
            }
            if(!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
                params.pageSize = options.pageSize;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EAE.QUERY.url,
                MS._config.parameters.EAE.QUERY.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    apply(options) {
        var params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.applicantId) && isString(options.applicantId)) {
                params.applicantId = options.applicantId;
            }
            if(!isUndefined(options.applicantName) && isString(options.applicantName)){
                params.applicantName = options.applicantName;
            }
            if(!isUndefined(options.certificateOfRegistry) && isString(options.certificateOfRegistry)){
                params.certificateOfRegistry = options.certificateOfRegistry;
            }
            if(!isUndefined(options.invoice) && isString(options.invoice)) {
                params.invoice = options.invoice;
            }
            if(!isUndefined(options.surveyReport) && isString(options.surveyReport)) {
                params.surveyReport = options.surveyReport;
            }
            if(!isUndefined(options.driverLicence) && isString(options.driverLicence)) {
                params.driverLicence = options.driverLicence;
            }
            if(!isUndefined(options.portClearance) && isString(options.portClearance)){
                params.portClearance = options.portClearance;
            }
            if(!isUndefined(options.navigationalPlanning) && isString(options.navigationalPlanning)){
                params.navigationalPlanning = options.navigationalPlanning;
            }
            if(!isUndefined(options.insuranceCertificate) && isString(options.insuranceCertificate)) {
                params.insuranceCertificate = options.insuranceCertificate;
            }
            if(!isUndefined(options.crewGoods) && isString(options.crewGoods)) {
                params.crewGoods = options.crewGoods;
            }
            if(!isUndefined(options.shippingGoods) && isString(options.shippingGoods)) {
                params.shippingGoods = options.shippingGoods;
            }
            if(!isUndefined(options.type) && isNumber(options.type)) {
                params.type = options.type;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EAE.CREATE.url,
                MS._config.parameters.EAE.CREATE.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    remove(id) {
        var params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EAE.REMOVE.url,
                MS._config.parameters.EAE.REMOVE.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    submit(id) {
        var params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EAE.SUBMIT.url,
                MS._config.parameters.EAE.SUBMIT.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    back(id) {
        var params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EAE.BACK.url,
                MS._config.parameters.EAE.BACK.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    pass(id) {
        var params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EAE.PASS.url,
                MS._config.parameters.EAE.PASS.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    cancel(id) {
        var params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EAE.CANCEL.url,
                MS._config.parameters.EAE.CANCEL.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    appendCrewInfo(options) {
        var params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.eaeId) && isString(options.eaeId)) {
                params.eaeId = options.eaeId;
            }
            if(!isUndefined(options.name) && isString(options.name)){
                params.name = options.name;
            }
            if(!isUndefined(options.age) && isNumber(options.age)){
                params.age = options.age;
            }
            if(!isUndefined(options.gender) && isNumber(options.gender)) {
                params.gender = options.gender;
            }
            if(!isUndefined(options.idCardA) && isString(options.idCardA)) {
                params.idCardA = options.idCardA;
            }
            if(!isUndefined(options.idCardB) && isString(options.idCardB)) {
                params.idCardB = options.idCardB;
            }
            if(!isUndefined(options.passCard) && isString(options.passCard)){
                params.passCard = options.passCard;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EAE.APPEND_CREW_INFO.url,
                MS._config.parameters.EAE.APPEND_CREW_INFO.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    updateCrewInfo(options) {
        var params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.crewInfoId) && isString(options.crewInfoId)) {
                params.crewInfoId = options.crewInfoId;
            }
            if(!isUndefined(options.name) && isString(options.name)){
                params.name = options.name;
            }
            if(!isUndefined(options.age) && isNumber(options.age)){
                params.age = options.age;
            }
            if(!isUndefined(options.gender) && isNumber(options.gender)) {
                params.gender = options.gender;
            }
            if(!isUndefined(options.idCardA) && isString(options.idCardA)) {
                params.idCardA = options.idCardA;
            }
            if(!isUndefined(options.idCardB) && isString(options.idCardB)) {
                params.idCardB = options.idCardB;
            }
            if(!isUndefined(options.passCard) && isString(options.passCard)){
                params.passCard = options.passCard;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EAE.UPDATE_CREW_INFO.url,
                MS._config.parameters.EAE.UPDATE_CREW_INFO.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    removeCrewInfo(crewInfoId) {
        var params = {crewInfoId:crewInfoId}
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EAE.REMOVE_CREW_INFO.url,
                MS._config.parameters.EAE.REMOVE_CREW_INFO.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    loadEntryAndExitType() {
        var params = {}
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EAE.LOAD_ENTRY_AND_EXIT_TYPE.url,
                MS._config.parameters.EAE.LOAD_ENTRY_AND_EXIT_TYPE.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    loadEntryAndExitStatus() {
        var params = {}
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EAE.LOAD_ENTRY_AND_EXIT_STATUS.url,
                MS._config.parameters.EAE.LOAD_ENTRY_AND_EXIT_STATUS.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }
}

module.exports = entryAndExit